package com.guessdroid.sensing;

import java.util.ArrayList;
import java.util.List;

import android.net.wifi.ScanResult;

public class RawData {
	public ArrayList<Float> lightData;
	public ArrayList<Integer> lightStamps;
	public ArrayList<Float> accData;
	public ArrayList<Integer> accStamps;
	public ArrayList<short[]> auData;
	public ArrayList<Integer> auStamps;
	public List<ScanResult> wifiData=null;
	
	public RawData()
	{
		lightData=new ArrayList<Float>();
		lightStamps=new ArrayList<Integer>();
		accData=new ArrayList<Float>();
		accStamps=new ArrayList<Integer>();
		auData=new ArrayList<short[]>();
		auStamps=new ArrayList<Integer>();
	}
	
	// Will remove early data and adjust timestamps accordingly. Because early data is bad, apparently.
	// Throws exception if measurement had no time to collect even a single light event
	public void preProcess() throws Exception
	{
		if (auData.size()==0||auStamps.size()==0||lightData.size()==0
				||lightStamps.size()==0) {throw new Exception();}
		
		// Fix audio and get the likely offset for good data
		auData.remove(0);
		int offset=auStamps.get(0);
		auStamps.remove(0);
		int i=0;
		while (i<auStamps.size()) // useless if auData frequency is trusted
		{
			auStamps.set(i, auStamps.get(i)-offset);
			i++;
		}
		
		// Fix light
		i=0;
		while (i<lightStamps.size())
		{
			if (lightStamps.get(i)>=offset) {break;}
			i++;
		}
		while (i>1)
		{
			lightData.remove(0);
			lightStamps.remove(0);
			i--;
		}
		lightStamps.set(0, 0);
		i=1;
		while (i<lightStamps.size())
		{
			lightStamps.set(i,lightStamps.get(i)-offset);
			i++;
		}
		
		// Fix acc
		i=0;
		while (i<accStamps.size())
		{
			accStamps.set(i, accStamps.get(i)-offset);
			i++;
		}
		while (accStamps.size()>2) // groups of 3
		{
			if (accStamps.get(2)<0)
			{
				accStamps.remove(0);accStamps.remove(1);accStamps.remove(2);
				accData.remove(0);accData.remove(1);accData.remove(2);
			}
			else {break;}
		}
		if (accStamps.size()<3)
		{
			if (accStamps.size()>1) {accStamps.set(1,0);}
			if (accStamps.size()>0) {accStamps.set(0,0);}
		}
		else
		{
			if (accStamps.get(1)<0) {accStamps.set(1,0);}
			if (accStamps.get(0)<0) {accStamps.set(0,0);}
		}
	}
}
